package com.kye.express.business.help.search;

import android.content.Context;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.help.RequestQueryHelpHotList;
import com.kye.model.bean.help.RequestQueryHelpList;
import com.kye.model.bean.help.ResultQueryHelpHotListBean;
import com.kye.model.bean.help.ResultQueryHelpListBean;
import com.kye.model.datasource.HelpDataSource;
import com.kye.model.storage.HelpHistorySearchDataSource;

import java.util.List;

import io.reactivex.MaybeObserver;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

/**
 * 帮助-搜索 Presenter
 * <p>
 * Created by SkyYu on 2018/1/9
 */
public interface HelpSearchContract {

    interface HelpSearchView extends IView {

        void onHotSuccess(OpenApiResult<ResultQueryHelpHotListBean> result);

        void onHotApiError(OpenApiResult<ResultQueryHelpHotListBean> result, int code, String msg);

        void onHotOtherError(Throwable throwable);

        void onSearchSuccess(OpenApiResult<List<ResultQueryHelpListBean>> result);

        void onSearchApiError(OpenApiResult<List<ResultQueryHelpListBean>> result, int code, String msg);

        void onSearchOtherError(Throwable throwable);

        void onSearchHistorySuccess(List<String> data);

        void onSearchHistoryFailed(Throwable e);

        void onLabelClicked(String item);
    }

    class HelpSearchPresenter extends BasePresenter<HelpSearchView> {

        Context mContext;
        HelpDataSource helpDataSource;
        HelpHistorySearchDataSource helpHistorySearchDataSource;

        HelpSearchPresenter(Context context) {
            mContext = context;
            helpHistorySearchDataSource = new HelpHistorySearchDataSource();
            helpDataSource = new HelpDataSource();
        }

        /**
         * 网络请求——获取热门搜索
         */
        void requestTpQueryHotSearch() {
            helpDataSource.queryHotSearch(new RequestQueryHelpHotList())
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultQueryHelpHotListBean>(this) {
                        @Override
                        public void onStart(Disposable d) {
                        }

                        @Override
                        public void onLoaded() {
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultQueryHelpHotListBean> result, ResultQueryHelpHotListBean requestQueryHotModel) {
                            getView().onHotSuccess(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultQueryHelpHotListBean> result, int code, String msg) {
                            getView().onHotApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onHotOtherError(e);
                        }
                    });
        }

        /**
         * 网络请求——获取搜索后帮助列表信息接口
         */
        void requestToQueryHelpInfoList(String ids, int classId, int num, String keyword) {
            helpDataSource.queryHelpInfoList(new RequestQueryHelpList(ids, classId, num, keyword))
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<List<ResultQueryHelpListBean>>(this) {
                        @Override
                        public void onStart(Disposable d) {
                        }

                        @Override
                        public void onLoaded() {
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<ResultQueryHelpListBean>> result, List<ResultQueryHelpListBean> bean) {
                            getView().onSearchSuccess(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<ResultQueryHelpListBean>> result, int code, String msg) {
                            getView().onSearchApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onSearchOtherError(e);
                        }
                    });
        }

        boolean saveQueryData(String queryData) {
            return helpHistorySearchDataSource.setSearchData(queryData);
        }

        /**
         * 创建数据保存到历史记录的观察者
         */
        void saveQueryDataObservable(String queryData) {
            Observable.just(queryData)
                    .compose(getView().bindToLifecycleDestroy())
                    .map(new Function<String, Boolean>() {
                        @Override
                        public Boolean apply(String s) throws Exception {
                            return saveQueryData(s);
                        }
                    })
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onNext(Boolean saveSuccess) {
                            if (saveSuccess) {
                                updateSearchHistoryData();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        /**
         * 把搜索过的数据保存到历史记录集合
         */
        void updateSearchHistoryData() {
            helpHistorySearchDataSource.getQueryDataRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<String>>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(List<String> strings) {
                            getView().onSearchHistorySuccess(strings);
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onSearchHistoryFailed(e);
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        /**
         * 清空历史记录集合
         */
        void clearSearchHistoryData() {
            helpHistorySearchDataSource.removeQueryDataRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .filter(new Predicate<Boolean>() {
                        @Override
                        public boolean test(Boolean success) throws Exception {
                            return success;
                        }
                    })
                    .map(new Function<Boolean, List<String>>() {
                        @Override
                        public List<String> apply(Boolean aBoolean) throws Exception {
                            return helpHistorySearchDataSource.getQueryData();
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new MaybeObserver<List<String>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(List<String> strings) {
                            getView().onSearchHistorySuccess(strings);
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onSearchHistoryFailed(e);
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }
}